import 'package:boss/color_constant.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/textStyle/text_style.dart';
import 'package:boss/ui/page/commodity/categories/modal/changeClassificationModal/change_classification_modal.dart';
import 'package:boss/ui/page/commodity/classificationManagement/classification_management_page.dart';
import 'package:boss/ui/page/index/model/bottom_navigation.dart';
import 'package:boss/ui/page/index/widget/custom_bottom_navigation_bar_item.dart';
import 'package:boss/ui/widget/button/rect_button.dart';
import 'package:boss/ui/widget/menu/menu.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

class CategoriesFooter extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Consumer<CommodityProvider>(
      builder: (context, commodityProvider, child) {
        final btnHeight = 98.w + MediaQuery.of(context).padding.bottom;
        final padding = EdgeInsets.only(bottom: MediaQuery.of(context).padding.bottom);

        switch (commodityProvider.batchType) {
          case BatchType.delete:
            return RectButton(
              title: '删 除',
              height: btnHeight,
              padding: padding,
              onTap: () {
                var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);
                commodityProvider.deleteCommoditysById();
              },
            );
          case BatchType.changeClassification:
            return RectButton(
              title: '移动至',
              height: btnHeight,
              padding: padding,
              onTap: () {
                var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);
                if (commodityProvider.selectedCommodities.length == 0) {
                  toast('请选择菜品');
                  return;
                }

                showChangeClassificationModal(context);
              },
            );
          default:
            return Stack(
              overflow: Overflow.visible,
              children: <Widget>[
                Positioned(
                  top: -38.w,
                  left: 0,
                  right: 0,
                  child: CustomPaint(
                    size: Size(double.infinity, 134.w + MediaQuery.of(context).padding.bottom),
                    painter: CatergoriesFooterBg(),
                  ),
                ),
                Container(
                  width: double.infinity,
                  height: btnHeight,
                  padding: padding,
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.spaceAround,
                    children: [
                      CustomBottomNavigationBarItem(
                        data: BottomNavigation(icon: 'assets/image/class_icon.png', title: '分类管理'),
                        onTap: () {
                          Navigator.pushNamed(context, ClassificationManagementPage.pageName);
                        },
                      ),
                      CustomBottomNavigationBarItem(
                        data: BottomNavigation(icon: 'assets/image/class_icon.png', title: '手动新增'),
                      ),
                      Menu(
                        builder: (context, isShow) {
                          return CustomBottomNavigationBarItem(
                            isSelected: isShow,
                            selectedTextStyle: TextStyleConstant().blue_20,
                            data: BottomNavigation(icon: 'assets/image/operating_icon.png', selectedIcon: 'assets/image/operating_on_icon.png', title: '批量操作'),
                          );
                        },
                        items: ['改分类', '删除'],
                        onMenuItemTap: (index) {
                          var types = {0: BatchType.changeClassification, 1: BatchType.delete};

                          var commodityProvider = Provider.of<CommodityProvider>(context, listen: false);
                          commodityProvider.setBatchType(types[index]);
                        },
                      ),
                    ],
                  ),
                ),
              ],
            );
        }
      },
    );
  }
}

class CatergoriesFooterBg extends CustomPainter {
  final outTop = 38.w;
  final circleRadius = 120.w;

  @override
  void paint(Canvas canvas, Size size) {
    final path = Path();
    double nextX;
    double nextY;

    nextX = 0;
    nextY = outTop;
    path.moveTo(nextX, nextY);

    nextX = (size.width - circleRadius) / 2;
    path.lineTo(nextX, nextY);

    // path.cubicTo(nextX + 10, 0, (size.width + circleRadius) / 2 - 10, 0, (size.width + circleRadius) / 2, outTop);

    path.quadraticBezierTo(size.width / 2, -outTop / 2, (size.width + circleRadius) / 2, nextY);

    nextX = size.width;
    path.lineTo(nextX, nextY);

    nextY = size.height;
    path.lineTo(nextX, nextY);

    nextX = 0;
    path.lineTo(nextX, nextY);

    path.close();

    canvas.drawShadow(path.shift(Offset(0, -2)), ColorConstant.e5e5, 10, true);

    canvas.drawPath(
        path,
        Paint()
          ..color = Colors.white
          ..strokeWidth = 1
          ..style = PaintingStyle.fill);
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
